package com.kaifamiao.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;

public class JdbcTest03 {

    public static void main(String[] args) throws SQLException {

        DbHelper dh = new DbHelper();
        // 加载驱动
        dh.load();
        // 建立连接
        Connection c = dh.connect();
        System.out.println( c );

        // 创建用于 发送 SQL 和 接受结果的 Statement 对象
        Statement st = c.createStatement();

        ResultSet rs = st.executeQuery( "SELECT empno , ename , job , sal FROM emp " );

        final String pattern = "大家好，我是{0}号员工{1}，我在公司从事{2}工作，月薪{3}元" ;

        while( rs.next() ){
            int eno = rs.getInt( "empno" );
            String name = rs.getString( "ename" );
            String job = rs.getString( "job" );
            double salary = rs.getDouble( "sal" );
            // 消息格式化
            String s = MessageFormat.format( pattern , eno , name , job , salary );
            System.out.println( s );
        }

        // 释放资源
        dh.release( st );
        dh.release( c );

    }
}
